package com.bit101.components
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;


	/**
	 * 树组件
	 * @author feng 2016-3-21
	 */
	public class Tree extends List
	{
		/**
		 * 基础数据
		 */
		private var baseItems:Array = [];

		/**
		 * 构建树组件
		 * @param parent			父容器
		 * @param xpos				x坐标
		 * @param ypos				y坐标
		 * @param items				数据
		 */
		public function Tree(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, items:Array = null)
		{
			baseItems = items;
			var newItems:Array = toTreeItems(items);

			super(parent, xpos, ypos, newItems);
			listItemClass = TreeItem;

			autoHideScrollBar = true;

			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}

		/**
		 * 被添加到舞台事件
		 */
		protected function onAddedToStage(event:Event):void
		{
			addEventListener(Event.OPEN, onItemOpen);
		}

		/**
		 * 被移出舞台事件
		 */
		protected function onRemovedFromStage(event:Event):void
		{
			removeEventListener(Event.OPEN, onItemOpen);
		}

		/**
		 * 树item显示对象打开或关闭节点事件
		 */
		protected function onItemOpen(event:Event):void
		{
			var item:TreeItem = event.target as TreeItem;
			if (item.open)
			{
				openItem(item.data);
			}
			else
			{
				closeItem(item.data);
			}
		}

		/**
		 * 开启节点
		 * @param itemData
		 */
		private function openItem(itemData:Object):void
		{
			var children:Array = itemData.data.children;
			var startIndex:int = items.indexOf(itemData) + 1;
			if (children == null)
				return;

			var i:int = 0;
			var childrenData:Array = itemData.childrenData;
			if (childrenData == null)
			{
				childrenData = itemData.childrenData = [];
				var itemDepth:int = itemData.depth;
				for (i = 0; i < children.length; i++)
				{
					childrenData.push({data: children[i], depth: itemDepth + 1, parent: itemData});
				}
			}
			for (i = 0; i < childrenData.length; i++)
			{
				super.addItemAt(childrenData[i], startIndex + i);
			}
			for (i = 0; i < childrenData.length; i++)
			{
				if (childrenData[i].open)
				{
					openItem(childrenData[i]);
				}
			}
		}

		/**
		 * 关闭节点
		 * @param itemData		节点数据
		 */
		private function closeItem(itemData:Object):void
		{
			var childrenData:Array = itemData.childrenData;
			var i:int = 0;
			for (i = 0; i < childrenData.length; i++)
			{
				var childData:Object = childrenData[i];
				if (childData.open)
				{
					closeItem(childData);
				}
				super.removeItem(childData);
			}
		}

		/**
		 * 添加节点
		 */
		override public function addItem(item:Object):void
		{
			baseItems.push(item);
			super.addItem({data: item});
		}

		private function $addItemAt(item:Object, index:int, depth:int):void
		{
			baseItems.splice(index, 0, item);
			super.addItemAt({data: item, depth: depth}, index);
		}

		override public function addItemAt(item:Object, index:int):void
		{
			baseItems.splice(index, 0, item);
			super.addItemAt({data: item}, index);
		}

		override public function set items(value:Array):void
		{
			baseItems = value;
			var newItems:Array = toTreeItems(value);
			super.items = newItems;
		}

		override public function removeAll():void
		{
			baseItems.length = 0;
			super.removeAll();
		}

		override public function removeItem(item:Object):void
		{
			var index:int = baseItems.indexOf(item);
			removeItemAt(index);
		}

		override public function removeItemAt(index:int):void
		{
			if (index < 0 || index >= _items.length)
				return;
			baseItems.splice(index, 1);
			super.removeItemAt(index);
		}

		private function toTreeItems(value:Array):Array
		{
			if (value == null)
				return null;
			var newItems:Array = [];
			for (var i:int = 0; i < value.length; i++)
			{
				newItems[i] = {data: value[i]};
			}
			return newItems;
		}
	}
}

